package com.virgo.demo.support.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.demo.support.service.SystemRightCheckService;
import com.virgo.demo.system.domain.AdminRoleDo;
import com.virgo.demo.system.mapper.AdminRoleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SystemRightCheckServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRoleDo> implements SystemRightCheckService {

	/**
	 * 校验管理员权限
	 * @param userId     Long
	 * @param roleId     Long
	 * @param checkPoint String 多个节点以英文逗号分隔,节点之间是或多关系
	 * @return Boolean
	 */
	@Override
	public Boolean checkAdminRight(Long userId, Long roleId, String checkPoint) {
		if (roleId.compareTo(0L) < 1) {
			//小于等于0，说明是超管
			return true;
		}
		AdminRoleDo adminRoleDo = getById(roleId);
		if (ObjectUtils.isEmpty(adminRoleDo) || adminRoleDo.getStatus().equals(0) || ObjectUtils.isEmpty(adminRoleDo.getRightsList())) {
			return false;
		}
		String[] pointArr = checkPoint.split(",");
		List<String> rightList = Arrays.asList(adminRoleDo.getRightsList().split(","));
		for (String point : pointArr) {
			if (rightList.contains(point)) {
				return true;
			}
		}
		return false;
	}
}